package com.example.Administrator.shoujiweishi.activities.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.Administrator.shoujiweishi.MyApplication;
import com.example.Administrator.shoujiweishi.R;
import com.example.Administrator.shoujiweishi.activities.Insert_contacts;
import com.example.Administrator.shoujiweishi.utils.CircleTextImageView;
import com.example.Administrator.shoujiweishi.vo.Contact;
import com.github.lzyzsd.randomcolor.RandomColor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/6/13 0013.
 */

public class Twofragadapter extends RecyclerView.Adapter<Twofragadapter.ViewHolder>{
    String edtext="";
    static HashMap<String,Contact> hashMap=new HashMap();
    private List<Contact> list;
    private View itemView;

    public Twofragadapter(List<Contact> list){
        this.list=list;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView name,phonenum,address;
        private CircleTextImageView namep;

        public ViewHolder(View itemView) {
            super(itemView);
            name= (TextView) itemView.findViewById(R.id.name);
            address= (TextView) itemView.findViewById(R.id.address);
            phonenum= (TextView) itemView.findViewById(R.id.phonenum);
            namep= (CircleTextImageView) itemView.findViewById(R.id.namep);

        }
    }
    @Override
    public Twofragadapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        itemView = LayoutInflater.from(MyApplication.getContext())
                .inflate(R.layout.showcontact_item,parent,false);

        Twofragadapter.ViewHolder viewHolder = new Twofragadapter.ViewHolder(itemView);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final Twofragadapter.ViewHolder holder, int position) {
        final Contact contact=list.get(position);

        holder.name.setText(contact.getName());
        holder.address.setText(contact.getAddress());
        holder.phonenum.setText(contact.getPhonenum().replace(" ","").replace("-",""));
        holder.namep.setText(contact.getName().substring(1));
        RandomColor randomColor=new RandomColor();
        int color=randomColor.randomColor();
        holder.namep.setFillColor(color);
//        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked){
//                    if (contact.getPhonenum().replace(" ","").replace("-","").length()==11||contact.getPhonenum().matches("^1[34578]\\d{9}$")){
//                        if (hashMap.size()<3){
//                            hashMap.put(contact.getPhonenum(),contact);
//                            Insert_contacts.textView.setText("选择联系人（"+hashMap.size()+"）");
//                            edtext=edtext+contact.getPhonenum()+",";
//                            Insert_contacts.insertcontacts.setText(edtext);
//                            Toast.makeText(MyApplication.getContext(), hashMap.get(contact.getPhonenum()).getName()+"\n"+hashMap.get(contact.getPhonenum()).getAddress()+"\n"+hashMap.get(contact.getPhonenum()).getPhonenum(), Toast.LENGTH_SHORT).show();
//                        }else{
//                            Toast.makeText(MyApplication.getContext(), "最多选三个", Toast.LENGTH_SHORT).show();
//                            holder.checkBox.setChecked(false);
//                        }
//                    }else{
//                        holder.checkBox.setChecked(false);
//                    }
//
//                }else{
//                    hashMap.remove(contact.getPhonenum());
//                    Insert_contacts.textView.setText("选择联系人（"+hashMap.size()+"）");
//                    edtext=edtext.replace(contact.getPhonenum()+",","");
//                    Insert_contacts.insertcontacts.setText(edtext);
//
//                }
//            }
//        });
//        itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(MyApplication.getContext(), "点击事件", Toast.LENGTH_SHORT).show();
//                holder.checkBox.setChecked(true);
//            }
//        });
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
}